﻿using System;
using System.Collections;
using System.Collections.Generic;
using Unity.Mathematics;
using UnityEngine;

#if UNITY_EDITOR
[UnityEditor.CanEditMultipleObjects]
#endif
public class BattleSceneLandscapeRegion : MonoBehaviour
{
    public void CopyFromScene()
    {
        var landScapeRoot = transform.Find("root");
        if (landScapeRoot != null)
        {
            GameObject.DestroyImmediate(landScapeRoot.gameObject);
        }
        var landScapeRootGameObject = new GameObject("root");
        landScapeRoot = landScapeRootGameObject.transform;
        landScapeRoot.transform.SetParent(transform);
        landScapeRoot.transform.localPosition = Vector3.zero;
        landScapeRoot.transform.forward = Vector3.forward;
        var selfCollider = GetComponent<BoxCollider>();
        var selfBound = selfCollider.bounds;
        var streetRoot = GameObject.Find("[Root]/Street");
        if (streetRoot)
        {
            var colliders = streetRoot.gameObject.GetComponentsInChildren<Collider>();
            for (int i = 0; i < colliders.Length; i++)
            {
                if (colliders[i].bounds.Intersects(selfBound))
                {
                    var newObject = GameObject.Instantiate(colliders[i].gameObject, landScapeRoot);
                    newObject.transform.position = colliders[i].gameObject.transform.position;
                    newObject.transform.forward = colliders[i].gameObject.transform.forward;
                }
            }
        }
    }
}
